import * as api from '../api/api';
import request from '../utils/request';
import { pageSize } from '../utils/common';
export default {
    namespace: 'dividend',
    state: {
        dividend: {},
        dividends: { totalCount: 0, items: [] },
        dividendSearchs: { pageIndex: 1, pageSize },
    },

    effects: {
        /**
              * 获取分红比例列表信息
              * @param param0 
              * @param param1 
              */
        *getDividends({ payload }, { call, put }) {
            const { pageIndex, pageSize } = payload;
            const formSkipCount = (pageIndex - 1) * pageSize,
                formMaxResultCount = pageSize;
            const dividends = yield request(api.ManagedividendManageApiFetchParamCreactor.managedividendManageGetDividendsAsync({
                formSkipCount, formMaxResultCount
            }))
            yield put({ type: 'save', payload: { dividends, dividendSearchs: { pageIndex, pageSize } } });
        },

        /**
         * 保存分红比例
         * @param param0 
         * @param param1 
         */
        *saveDividends({ payload }, { call, select, put }) {
            const { form, callback } = payload;
            const { dividendSearchs } = yield select(({ dividend }) => dividend);
            yield request(api.ManagedividendManageApiFetchParamCreactor.managedividendManageSaveDividendsAsync({ form }));
            yield put({ type: 'getDividends', payload: { ...dividendSearchs } });

            callback && callback(true);
        },
    },
    reducers: {
        save(state, action) {
            return { ...state, ...action.payload };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, search }) => {
                switch (pathname) {
                    case '/dividend/dividends':
                        dispatch({ type: 'getDividends', payload: { pageIndex: 1, pageSize } });
                        break;
                }
            });
        },
    },
};
